package com.skyable.device.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.skyable.common.common.ServiceResult;
import com.skyable.common.utils.UUIDUtils;
import com.skyable.device.entity.DeveloperKey;
import com.skyable.device.mapper.DeveloperKeyMapper;
import com.skyable.device.service.IDeveloperKeyService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author GaoYuan
 * @date 2019/11/14 10:01
 * @desc
 */
@Service
public class DeveloperKeyServiceImpl extends ServiceImpl<DeveloperKeyMapper,DeveloperKey> implements IDeveloperKeyService {

    @Override
    public ServiceResult<Integer> createDeveloperKey(Integer domainId, String userName) {
        ServiceResult<Integer> serviceResult = new ServiceResult<>();
        DeveloperKey developerKey = new DeveloperKey();
        developerKey.setDeveloperSK(UUIDUtils.getUUID());
        developerKey.setDeveloperAK(UUIDUtils.getUUID());
        developerKey.setDomainId(domainId);
        //状态 0：启用  1：不启用
        developerKey.setStatus(0);
        developerKey.setCreateTime(new Date());
        developerKey.setUpdateTime(new Date());
        developerKey.setCreateUser(userName);
        int result = baseMapper.insert(developerKey);
        serviceResult.setResult(result);
        return serviceResult;
    }

    @Override
    public ServiceResult<Integer> deleteDeveloperKey(Long id) {
        ServiceResult<Integer> serviceResult = new ServiceResult<>();
        int result = baseMapper.deleteById(id);
        serviceResult.setResult(result);
        return serviceResult;
    }

    @Override
    public ServiceResult<Integer> stopDeveloperKey(Long id) {
        ServiceResult<Integer> serviceResult = new ServiceResult<>();
        DeveloperKey developerKey = baseMapper.selectById(id);
        developerKey.setStatus(1);
        int result = baseMapper.updateById(developerKey);
        serviceResult.setResult(result);
        return serviceResult;
    }

    @Override
    public ServiceResult<List<DeveloperKey>> DeveloperKeyFindAll(Integer domainId) {
        ServiceResult<List<DeveloperKey>> serviceResult = new ServiceResult<>();
        List<DeveloperKey> developerKeyList = lambdaQuery().eq(DeveloperKey::getDomainId, domainId).list();
        serviceResult.setResult(developerKeyList);
        return serviceResult;
    }

    @Override
    public ServiceResult<Integer> startDeveloperKey(Long id) {
        ServiceResult<Integer> serviceResult = new ServiceResult<>();
        DeveloperKey developerKey = baseMapper.selectById(id);
        developerKey.setStatus(0);
        int result = baseMapper.updateById(developerKey);
        serviceResult.setResult(result);
        return serviceResult;
    }

    @Override
    public ServiceResult<DeveloperKey> developerKeyQueryByAK(String developerAK) {
        ServiceResult<DeveloperKey> serviceResult = new ServiceResult<>();
        DeveloperKey developerKey = lambdaQuery().eq(DeveloperKey::getDeveloperAK, developerAK).one();
        serviceResult.setResult(developerKey);
        return serviceResult;
    }
}
